package utils

import "net/http"

type ErrorType struct {
	StatusCode int
	ErrorCode  int
	ErrorMsg   string
}

func MakeError(err error) *ErrorType {
	return &ErrorType{
		StatusCode: http.StatusInternalServerError,
		ErrorCode:  50000,
		ErrorMsg:   err.Error(),
	}
}

func NewError(status, errcode int, errmsg string) (err *ErrorType) {
	return &ErrorType{
		StatusCode: status,
		ErrorCode:  errcode,
		ErrorMsg:   errmsg,
	}
}

func NewPaging(currentPage, pageSize, total int) *Paging {
	totalPage := total / pageSize
	if total%pageSize != 0 {
		totalPage += 1
	}

	return &Paging{
		Total:       total,
		TotalPage:   totalPage,
		CurrentPage: currentPage,
		PageSize:    pageSize,
	}
}

type Paging struct {
	Total       int `json:"total"`
	TotalPage   int `json:"totalPage"`
	CurrentPage int `json:"currentPage"`
	PageSize    int `json:"pageSize"`
}

type Meta struct {
	ErrCode int     `json:"errCode"`
	ErrMsg  string  `json:"errMsg,omitempty"`
	Paging  *Paging `json:"paging"`
}

type ResApi2 struct {
	Meta *Meta       `json:"meta"`
	Data interface{} `json:"data"`
}

func NewMetaPagingApi(currentPage, pageSize, total int) *Meta {
	return &Meta{
		ErrCode: 0,
		ErrMsg:  "success",
		Paging:  NewPaging(currentPage, pageSize, total),
	}
}

func NewDefaultMetaApi() *Meta {
	return &Meta{
		ErrCode: 0,
		ErrMsg:  "success",
	}
}

func newResApi(data interface{}) *ResApi2 {
	return &ResApi2{
		Meta: NewDefaultMetaApi(),
		Data: data,
	}
}

func newErrApi(errCode int, errMsg string) *ResApi2 {
	return &ResApi2{
		Meta: NewMetaApi(errCode, errMsg),
	}
}

func NewMetaApi(errCode int, errMsg string) *Meta {
	return &Meta{
		ErrCode: errCode,
		ErrMsg:  errMsg,
	}
}
